import { MessagePlugin } from "tdesign-vue-next";
import { BaseServices } from "./base.services";
import { APIUrl, APIMethod } from "./request.services";


class EmailService extends BaseServices {
  private static _ins: EmailService = new EmailService();
  public static get ins(): EmailService {
    return this._ins ? this._ins : this._ins = new EmailService();
  }

  /** 邮箱 发送注册验证码 */
  public sendVerifyCode(email: string): Promise<{ secret?: string }> {
    const url = APIUrl.Email_Register + APIMethod.Code;
    const data = { email };
    return new Promise<{ secret?: string }>((reslove, reject) => {
      this.request(url, data).then(async (res: { secret: string }) => {
        MessagePlugin("success", "邮件发送成功");
        reslove(res);
      }).catch(err => {
        MessagePlugin("error", "邮件发送失败");
        reject(err)
      });
    });
  }

  /** 邮箱 发送找回密码验证码 */
  public sendForgotPasswordCode(email: string): Promise<{ secret?: string }> {
    const url = APIUrl.Email_Forgot_password + APIMethod.Code;
    const data = { email };
    return new Promise<{ secret?: string }>((reslove, reject) => {
      this.request(url, data).then(async (res: { secret: string }) => {
        MessagePlugin("success", "邮件发送成功");
        reslove(res);
      }).catch(err => {
        MessagePlugin("error", "邮件发送失败");
        reject(err)
      });
    });
  }
}

export const emailService: EmailService = EmailService.ins;